// pages/shortMsg/shortMsg.js
import * as API from '../../api/msgApi';
import * as util from '../../utils/util';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    dataSource: [],
    pageNo: 1,
    pageSize: 10,
    total: 0
  },

  clear: function () {
    // 清空数据重新加载
    this.setData({
      dataSource: [],
      pageNo: 1,
      pageSize: 10
    });
  },
  loadData: function (options) {
    API.getShortMsgList(this.data.pageNo, this.data.pageSize).then(data => {
      let arr = data.shortMessageList || [];
      if (!options) {
        arr = this.data.dataSource.concat(data.shortMessageList);
      }
      let hasMore = arr.length < data.totalCount;
      this.setData({
        dataSource: arr,
        total: data.totalCount,
        hasMore: hasMore
      });
      wx.stopPullDownRefresh()
    })
  },
  toDetail: function (e) {
    util.preventMoreClick(this, () => {
      let id = e.currentTarget.dataset.item.id;
      wx.navigateTo({
        url: './shortMsgDetail/shortMsgDetail?id=' + id
      });
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.clear();
    this.loadData(true);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 重新加载
    this.clear();
    this.loadData(false);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 增量加载
    var hasMore = this.data.dataSource.length < this.data.total;
    if (hasMore) {
      this.setData({
        pageNo: ++this.data.pageNo
      });
      this.loadData(false);
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});